package com.survey.wxapp.service.impl;

import com.survey.wxapp.domain.WeChatPhoneNumber;
import com.survey.wxapp.service.WeChatDecryptService;
import com.survey.wxapp.util.WeChatDecryptUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

@Service
public class WeChatDecryptServiceImpl implements WeChatDecryptService {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public  String getPhoneNumber(String encryptedData, String iv, String sessionKey) throws Exception {
        // 解密数据
        String decryptedData = WeChatDecryptUtils.decryptPhoneNumber(encryptedData, iv, sessionKey);

        // 解析 JSON 字符串，提取手机号
        WeChatPhoneNumber phoneNumberData = objectMapper.readValue(decryptedData, WeChatPhoneNumber.class);
        return phoneNumberData.getPhoneNumber();
    }
}
